var moduleCode = getQueryString("moduleCode");
$(function () {
    $("#jqGrid").Grid({
        url: '../modulemaintainrulesbase/list?moduleCode='+moduleCode,
        colModel: [
            {label: '规程名称', name: 'rulesName', index: 'RULES_NAME', width: 80},
            {label: '模具编号', name: 'moduleCode', index: 'MODULE_CODE', width: 80},
            {label: '项目序号', name: 'itemNo', index: 'ITEM_NO', width: 80},
            {label: '实施单位', name: 'impleUnitName', index: 'IMPLE_UNIT_NAME', width: 80},
            {label: '实施频率', name: 'impleFrequence', index: 'IMPLE_FREQUENCE', width: 80,
                formatter: function (value) {
                    return sysEnums.FRERATE[value];
                }
            },
            {label: '编制名称', name: 'organName', index: 'ORGAN_NAME', width: 80},
            {label: '创建人', name: 'createName', index: 'CREATE_USER', width: 80},
            {label: '创建时间', name: 'createDate', index: 'CREATE_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '状态', name: 'rulesStatus', index: 'RULES_STATUS', width: 80,
                formatter: function (value) {
                    return sysEnums.MAINTAIN_TYPE[value];
                }
            },//0-到期 1-正常 2-未开始 3-异常终止
            {label: '开始时间', name: 'startDate', index: 'START_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '结束时间', name: 'endDate', index: 'END_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '审核人名称', name: 'checkName', index: 'CHECK_USER', width: 80},
            {label: '审核状态', name: 'checkStatus', index: 'CHECK_STATUS', width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {label: '审核时间', name: 'checkDate', index: 'CHECK_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '复核人', name: 'reviewName', index: 'REVIEW_USER', width: 80},
            {label: '复核状态', name: 'reviewStatus', index: 'REVIEW_STATUS', width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {label: '复核时间', name: 'reviewDate', index: 'REVIEW_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '规程选项查看', name: 'id', index: 'ID', key: true,width: 80,
                formatter: function (value) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.goOption(\"" + value + "\")'><i class='fa fa-anchor'></i>&nbsp;维护规程选项</button>";
                }
            },]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        moduleMaintainRulesBase: {
            updateDate:'',
            updateUser:'',
            createDate:'',
            createUser:'',
            delFlag:'',
            reviewDate:'',
            reviewStatus:'',
            reviewUser:'',
            checkDate:'',
            checkStatus:'',
            checkUser:'',
            organName:'',
            impleFrequence:'',
            impleUnitName:'',
            itemNo:'',
            moduleCode:'',
            productLine:'',
            workshop:'',
            id:'',
            rulesStatus:'',
            endDate:'',
            startDate:'',
            checkResult: '',
            rulesName:''
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            moduleCode:'',
        },
        maintainType: enumsToList("MAINTAIN_TYPE"),
        freRateArray: enumsToList("FRERATE"),
    },
methods: {
    check: function(){

        var data = getSelectedRowData("#jqGrid");

        if(data.id == null){
            return;
        }

        if(data.checkStatus == '已审核'){
            alert("此条记录已审核", function () {
               vm.reload();
            })
            return;
        }

        openWindow({
            title: "确认审核",
            area: ['350px', '200px'],
            content: jQuery("#reviewLayer"),
            btn: ['确定', '取消'],
            btn1: function (index) {
                if(typeof (vm.moduleMaintainRulesBase.checkResult) == 'undefined'){
                    alert("请选择审核状态");
                    return;
                }
                vm.moduleMaintainRulesBase = {checkStatus: vm.moduleMaintainRulesBase.checkResult, id: data.id, moduleCode: moduleCode};
                var url = '../modulemaintainrulesbase/check';
                var params = JSON.stringify(vm.moduleMaintainRulesBase);
                vm.updateCheck(url, params)
            }
        });

    },
    meCheck: function(){

        var data = getSelectedRowData("#jqGrid");
        if(data.id == null){
            return;
        }

        if(data.checkStatus == '未审核'){
            alert("此条记录未通过车间授权人审核", function () {
            })
            return;
        }
        if(data.reviewStatus == '已审核'){
            alert("此条记录已复核", function () {
                vm.reload();
            })
            return;
        }

        openWindow({
            title: "确认审核",
            area: ['350px', '200px'],
            content: jQuery("#reviewLayer"),
            btn: ['确定', '取消'],
            btn1: function (index) {
                if(typeof (vm.moduleMaintainRulesBase.checkResult) == 'undefined'){
                    alert("请选择审核状态");
                    return;
                }
                vm.moduleMaintainRulesBase = {reviewStatus: vm.moduleMaintainRulesBase.checkResult, id: data.id, moduleCode: moduleCode };
                var url = '../modulemaintainrulesbase/meCheck';
                var params = JSON.stringify(vm.moduleMaintainRulesBase);
                vm.updateCheck(url, params)
            }
        });

    },
    updateCheck: function(url, params){
        Ajax.request({
            url: url,
            params: params,
            contentType: "application/json",
            type: 'POST',
            successCallback: function (res) {
                alert('审核成功!');
                setTimeout(function () {
                    location.reload();
                }, 1000);
            }
        });
    },
    //查看维护规程选项，这里应该读取modulemaintainrelate.html
    goOption: function(id){
        // var id = getSelectedRow("#jqGrid");
        if (id == null) {
            return;
        }
        openWindow({
            type: 2,
            title: '<i class="fa fa-bars"></i> 维护规程选项',//读取维护规程选项关联表数据
            content: '../module/modulemaintainrelate.html?whgcID=' + id
        });
    },
    goOptionList: function(){
        // var id = getSelectedRowData("#jqGrid");
        // var id = getSelectedRowData("#jqGrid").id;
        var id = getSelectedRow("#jqGrid");
        if (id == null) {
            return;
        }
        openWindow({
            type: 2,
            title: '<i class="fa fa-bars"></i> 维护规程关联选项',
            content: '../module/modulemaintainoptions_choose.html?whgcID='+id,
        });
    },
    query: function () {
        vm.reload();
    },
    add: function () {
        vm.showList = false;
        vm.title = "新增";
        vm.moduleMaintainRulesBase = {updateDate:'', updateUser:'', createDate:'', createUser:'', delFlag:'', reviewDate:'', reviewStatus:'', reviewUser:'', checkDate:'', checkStatus:'', checkUser:'',
            organName:'', impleFrequence:'', impleUnitName:'', itemNo:'', moduleCode: moduleCode, productLine:'', workshop:'', rulesStatus:'', endDate:'', startDate:''};
    },
    update: function (event) {
        var id = getSelectedRow("#jqGrid");
        if (id == null) {
            return;
        }
        vm.showList = false;
        vm.title = "修改";

        vm.getInfo(id);
    },
    saveOrUpdate: function (event) {
        var url = vm.moduleMaintainRulesBase.id == null ? "../modulemaintainrulesbase/save" : "../modulemaintainrulesbase/update";
        Ajax.request({
            url: url,
            params: JSON.stringify(vm.moduleMaintainRulesBase),
            type: "POST",
            contentType: "application/json",
            successCallback: function (r) {
                alert('操作成功', function (index) {
                    vm.reload();
                });
            }
        });
    },
    del: function (event) {
        var ids = getSelectedRows("#jqGrid");
        if (ids == null){
            return;
        }

        confirm('确定要删除选中的记录？', function () {
            Ajax.request({
                url: "../modulemaintainrulesbase/delete",
                params: JSON.stringify(ids),
                type: "POST",
                contentType: "application/json",
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        });
    },
    getInfo: function(id){
        Ajax.request({
            url: "../modulemaintainrulesbase/info/"+id,
            async: true,
            successCallback: function (r) {
                vm.moduleMaintainRulesBase = r.moduleMaintainRulesBase;
            }
        });
    },
    reload: function (event) {
        vm.showList = true;
        var page = $("#jqGrid").jqGrid('getGridParam', 'page');
        $("#jqGrid").jqGrid('setGridParam', {
            postData: {'name': vm.q.name,'moduleCode':vm.q.moduleCode},
            page: page
        }).trigger("reloadGrid");
        vm.handleReset('formValidate');
    },
    reloadSearch: function() {
        vm.q = {
            name: '',
            moduleCode:'',
        };
        vm.reload();
    },
    handleSubmit: function (name) {
        handleSubmitValidate(this, name, function () {
            vm.saveOrUpdate()
        });
    },
    handleReset: function (name) {
        handleResetForm(this, name);
    },
    getModuleArchives: function () {
        openWindow({
            type: 2,
            title: '<i class="fa fa-file-text-o"></i> 选择模具',
            content: '../module/moduleArchiveSelect.html',
            btn: ['确定', '取消'],
            btn1: function (index) {
                var iframeWin = window["layui-layer-iframe" + index];
                var res = iframeWin.getModuleArchives();
                if (!res) {
                    iview.Message.error("请选择一条记录");
                } else {
                    layer.close(index);
                    //模具编码
                    vm.moduleMaintainRulesBase.moduleCode = res.moduleCode;
                    //所属车间
                    vm.moduleMaintainRulesBase.workshop = res.subShopName;
                    // vm.moduleData.moduleType = res.moduleType;
                    // vm.moduleTypeName = res.moduleTypeName;
                }
            }
        })
    }
}
});